package xhwl.hmo.mall.db;


import android.content.Context;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import xhwl.hmo.mall.db.bean.Upgrade;
import xhwl.hmo.mall.event.UpgradeEvent;
import xhwl.hmo.mall.utils.StringUtil;


/**
* 升级模块数据库操作类
* @author lim
* @date 2017年2月9日
* @version 1.0
*/
public class UpgradeDao extends BaseDao<Upgrade> {

    public UpgradeDao(Context context) {
        super(context, DbConstant.Upgrade.DB_NAME, DbConstant.Upgrade.DB_VERSION);
        // TODO Auto-generated constructor stub
    }
    @Override
	public void onUpgrade(DbUtils db, int oldVersion, int newVersion) {
//        if(oldVersion==1&&newVersion==2){
//            try {
//                db.execNonQuery("alter table "+DbConstant.Upgrade.DB_NAME+" add "+DbConstant.Upgrade.COLUMN_FORCE_FLAG+" text default 0");
//            } catch (DbException e) {
//                e.printStackTrace();
//            }
//
//        }
    }
    @Override
    public Upgrade find(String versionCode) {
        // TODO Auto-generated method stub
        Upgrade record = null;
        if (!StringUtil.isEmpty(versionCode)) {
            try {
                record = db.findFirst(Selector.from(Upgrade.class).where(
                        DbConstant.Upgrade.COLUMN_VERSION_CODE, "=", versionCode)
                        .and(DbConstant.Upgrade.COLUMN_DOWNLOAD_STATE,"=", UpgradeEvent.STATE_COMPLETE));
            } catch (DbException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (record == null) {
            record=new Upgrade();
        }
        return record;
    }
}

